import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import Vant from 'vant'
import 'vant/lib/index.css'

Vue.use(ElementUI)
Vue.use(Vant)

// 对外暴露全局依赖，供子应用 iframe 复用
window.Vue = Vue
window.ELEMENT = ElementUI
window.Vant = Vant
window.axios = axios

Vue.config.productionTip = false

// On first load, merge URL search params into router query for hash mode
;(function syncSearchToRouter() {
  try {
    const search = window.location.search
    if (!search) return
    const params = new URLSearchParams(search)
    const queryFromSearch = {}
    params.forEach((value, key) => { queryFromSearch[key] = value })
    const current = router.currentRoute
    const mergedQuery = { ...queryFromSearch, ...(current && current.query ? current.query : {}) }
    const needsReplace = Object.keys(queryFromSearch).length > 0
    if (needsReplace) {
      router.replace({ path: current && current.path ? current.path : '/', query: mergedQuery })
      // Clean the search part from URL without reloading
      const newUrl = window.location.origin + window.location.pathname + window.location.hash
      window.history.replaceState({}, '', newUrl)
    }
  } catch (_) {}
})()

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')


